package com.tony.schedule;

import java.math.BigDecimal;
import java.util.List;
import java.util.Random;

import org.apache.log4j.Logger;
import org.springframework.util.CollectionUtils;

import com.tony.dao.AccountDAO;
import com.tony.dataobject.AccountDO;
import com.tony.service.AccountManager;
import com.tony.util.LoggerUtil;

/**
 * 每分钟为每个账号充值1-10元
 * 
 * @author hai.yuan
 * @version $Id: DepositScheduler.java, v 0.1 2013-6-2 上午4:43:22 hai.yuan Exp $
 */
public class DepositScheduler {

    private AccountDAO         accountDAO;

    private AccountManager     accountManager;

    public static final Random random = new Random();

    public static final Logger logger = Logger.getLogger("scheduler.log");

    public void depositAccount() {

        long start = System.currentTimeMillis();

        LoggerUtil.info(logger, "开始定时充值了,TimeKey=" + start);

        List<AccountDO> accountList = accountDAO.queryAll();

        if (CollectionUtils.isEmpty(accountList)) {
            return;
        }

        for (AccountDO accountDO : accountList) {

            try {

                BigDecimal depositAmount = new BigDecimal(random.nextInt(100));

                accountManager.deposit(accountDO.getEmail(), depositAmount);
            } catch (Exception e) {

                LoggerUtil.error(logger, "定时充值失败,TimeKey=" + start, e);
            }
        }

        LoggerUtil.info(logger, "定时充值完毕，耗时：" + (System.currentTimeMillis() - start) + "ms,TimeKey="
                                + start);

    }

    /**
     * Setter method for property <tt>accountManager</tt>.
     * 
     * @param accountManager value to be assigned to property accountManager
     */
    public void setAccountManager(AccountManager accountManager) {
        this.accountManager = accountManager;
    }

    /**
     * Setter method for property <tt>accountDAO</tt>.
     * 
     * @param accountDAO value to be assigned to property accountDAO
     */
    public void setAccountDAO(AccountDAO accountDAO) {
        this.accountDAO = accountDAO;
    }

}
